<?php

namespace App\Http\Controllers;

use App\Models\Category;
use Illuminate\Http\Request;
use App\Http\Requests\Category\StoreCategoryResource;
use App\Http\Requests\Category\UpdateCategoryResource;
use App\Http\Requests\Category\QueryCategoryCollection;
use App\Http\Requests\Category\DestroyCategoryResource;

class CategoryController extends Controller
{
	/**
	 * 获取分类集合
	 *
	 * Date: 2018/2/1
	 * @author George
	 * @param QueryCategoryCollection $request
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function index(QueryCategoryCollection $request)
    {
    	//定义分类作用域
    	$scope = $request->get('scope');
        // 定义关联关系
        $relation = $request->get('relation');

        $query = Category::query();

        //根据分类作用域获取响应的分类数据
        if ($scope) {
        	$query->where('scope', $scope);

            if($scope == 'task'){
                $query->with('tasks');
            }

            if($scope == 'shop'){
                $query->with('shops');
            }
		}

        // 加载关联关系
        if ($relation) {
        	$query->with($relation);
		}

        return $this->success($query->get());
    }


	/**
	 * 创建分类信息
	 *
	 * Date: 2018/2/2
	 * @author George
	 * @param StoreCategoryResource $request
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
    public function store(StoreCategoryResource $request)
    {
        $category = new Category();

        $attributes = $this->validate($request, [
            'name' => 'required',
            'scope' => 'required',
        ],[
            'name.required' => '分类名称不能为空',
            'scope.required' => '分类的作用域不能为空',
        ]);

        foreach ($attributes as $key => $value){
            $category->{$key} = $value;
        }
        $category->saveOrFail();
        return $this->stored($category);
    }

	/**
	 * 获取分类信息或分类绑定的任务
	 *
	 * Date: 2018/2/2
	 * @author George
	 * @param Request $request
	 * @param Category $category
	 * @return \Illuminate\Http\JsonResponse
	 */
    public function show(Request $request, Category $category)
    {
    	// 定义是否获取分类关联的任务
    	$tasks = $request->get('only_tasks');

    	if ($tasks) {
    		return $this->success($category->tasks);
		}

        return $this->success($category);
    }


	/**
	 * 更新分类信息
	 *
	 * Date: 2018/2/2
	 * @author George
	 * @param UpdateCategoryResource $request
	 * @param Category $category
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Throwable
	 */
    public function update(UpdateCategoryResource $request, Category $category)
    {
        $attributes = $this->validate($request, [
            'name' => 'required'
        ],[
            'name.required' => '分类名称不能为空',
        ]);

        foreach ($attributes as $k => $v){
            $category->{$k} = $v;
        }

        $category->saveOrFail();

        return $this->stored($category);
    }

	/**
	 * 删除分类信息
	 *
	 * Date: 2018/2/2
	 * @author George
	 * @param DestroyCategoryResource $request
	 * @param Category $category
	 * @return \Illuminate\Http\JsonResponse
	 * @throws \Exception
	 */
    public function destroy(DestroyCategoryResource $request, Category $category)
    {
        // 移除分类所有任务
        $category->tasks()->detach();

        if($category->delete()){
            return $this->deleted();
        }

        return $this->failed('删除失败');
    }

	/**
	 * 根据分类ID绑定任务到分类
	 *
	 * Date: 2018/2/2
	 * @author George
	 * @param UpdateCategoryResource $request
	 * @param Category $category
	 * @return \Illuminate\Http\JsonResponse
	 */
	public function attachTaskById(UpdateCategoryResource $request, Category $category)
	{
		$params = $this->validate($request, [
			'tasks' => 'required|array',
		]);

		try {
			//关联任务到分类
			$category->tasks()->sync($params['tasks']);
		} catch (\Exception $exception) {
			return $this->internalError('绑定失败');
		}

		return $this->success($category->tasks);
	}
}
